package com.leyou.item.controller;

import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import com.leyou.pojo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/9/29
 */
@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuByPage(
            String key, Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page
            , @RequestParam(value = "rows", defaultValue = "5") Integer rows

    ) {
        PageResult<SpuBo> result= goodsService.querySpuByPage(page,rows,saleable,key);
        if (result==null || result.getItems()==null ||result.getItems().isEmpty()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuBySpuId(@PathVariable("id") Long id){
        Spu spu=this.goodsService.querySpuBySpuId(id);
        if (spu!=null) {
            return ResponseEntity.ok(spu);
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuBo spuBo) {
        goodsService.saveGoods(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo) {
       goodsService.update(spuBo);

        return ResponseEntity.ok().build();
    }
    @PutMapping("spu/saleable/{id}")
    public ResponseEntity<Void> updateGoods(@PathVariable("id") Long id) {
        goodsService.updateSpuSaleable(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spu的id查询spudetail的信息
     * @param id
     * @return
     */
    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> querySpuDetailById(@PathVariable("id")Long id) {
       SpuDetail spuDetail= goodsService.querySpuDetailById(id);
        if (spuDetail!=null) {
            return ResponseEntity.ok(spuDetail);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);

    }

    /**
     * 根据spu的id查询sku的信息,顺便查询stock的信息
     * @param id
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id")Long id) {
       List<Sku> skuList= goodsService.querySkuBySpuId(id);
        if (skuList.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
        }
        return ResponseEntity.ok(skuList);
    }


}
